/**
 *  Copyright(C) 2007 Juri Strumpflohner
 *  All rights reserved
 *
 *  Created: 17.07.2007
*/

package com.aroundj2me.sandbox;

import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.StringItem;
import javax.microedition.location.Coordinates;
import javax.microedition.location.Criteria;
import javax.microedition.location.Location;
import javax.microedition.location.LocationException;
import javax.microedition.location.LocationProvider;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;

public class LocationMidletTest extends MIDlet implements CommandListener {
	private Display display;
	private Form f;
	private Command fetchLoc;


	public LocationMidletTest() {
		this.f = new Form("TestForm");
		fetchLoc = new Command("Fetch Location", Command.SCREEN, 1);
		f.addCommand(fetchLoc);
		f.setCommandListener(this);
	}

	protected void destroyApp(boolean arg0) throws MIDletStateChangeException {

	}

	protected void pauseApp() {
		
	}

	protected void startApp() throws MIDletStateChangeException {
		display = Display.getDisplay(this);
		display.setCurrent(f);
		retrieveLocation();
	}
	
	public void retrieveLocation(){
		f.deleteAll();
		
		Criteria cr = new Criteria();
		cr.setHorizontalAccuracy(500);
		
		try {
			LocationProvider pr = LocationProvider.getInstance(cr);
			
			Location l = pr.getLocation(60);
			Coordinates c = l.getQualifiedCoordinates();
					
			f.append(new StringItem("Cell id: ", String.valueOf(Location.MTE_CELLID)));
			f.append(new StringItem("Lat.: ", String.valueOf(c.getLatitude())));
			f.append(new StringItem("Long.: ", String.valueOf(c.getLongitude())));
//			f.append(new StringItem("Addr. info: ", l.getAddressInfo().toString()));
			f.append(new StringItem("Extra info: ", l.getExtraInfo("application/X-jsr179-location-lif")));
			
		} catch (LocationException e) {
			e.printStackTrace();
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		
		
	}

	public void commandAction(Command c, Displayable d) {
		if(c == fetchLoc){
			retrieveLocation();
		}
	}

}
